home *** CD-ROM | disk | FTP | other *** search
/ Risc World 3 / Risc World 3.iso / SOFTWARE / ISSUE6 / PD / PDF / DrawFile / c++ / GuiDrawFileSprite < prev    next >
Text File  |  2003-02-14  |  5KB  |  150 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //   Copyright (c) 2002, Colin Granville
  4. //
  5. //   All rights reserved.
  6. //
  7. //   Redistribution and use in source and binary forms, with or
  8. //   without modification, are permitted provided that the following 
  9. //   conditions are met:
  10. //
  11. //      * Redistributions of source code must retain the above copyright 
  12. //        notice, this list of conditions and the following disclaimer.
  13. //
  14. //      * Redistributions in binary form must reproduce the above 
  15. //        copyright notice, this list of conditions and the following 
  16. //        disclaimer in the documentation and/or other materials 
  17. //        provided with the distribution.
  18. //
  19. //      * The name Colin Granville may not be used to endorse or promote 
  20. //        products derived from this software without specific prior 
  21. //        written permission.
  22. //
  23. //   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  24. //   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  25. //   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
  26. //   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
  27. //   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
  28. //   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  29. //   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  30. //   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  31. //   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
  32. //   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  33. //   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
  34. //   OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //--------------------------------------------------------------------------
  37.  
  38. #include "GuiDrawFileSprite.h"
  39.  
  40. GuiDrawFileSprite::GuiDrawFileSprite(GuiDrawFile& d)
  41.  : drawfile(d)
  42. {
  43. }
  44.  
  45. //***************************************************************
  46.  
  47. GuiDrawFileSprite::~GuiDrawFileSprite()
  48. {
  49. }
  50.  
  51. //***************************************************************
  52.  
  53. void GuiDrawFileSprite::start(int width_pix,int height_pix,int bpp)
  54. {
  55.   startPos=drawfile.startObject(sizeof(GuiDrawFileGraphicsObject) + 
  56.                                 sizeof(GuiTransform) +
  57.                                 sizeof(GuiDrawFileSpriteBlock));
  58.  
  59.   if (drawfile.hasFailed()) return;
  60.  
  61.   sprite.offsetToMask=-1;
  62.   int* p=(int*)sprite.name;
  63.   p[0]= 's';p[1]=p[2]=0;
  64.   
  65.   if      (bpp>16) {bpp=32;sprite.mode = (6<<27) | (90<<14) | (90<<1) | 1;}
  66.   else if (bpp>8)  {bpp=16;sprite.mode = (5<<27) | (90<<14) | (90<<1) | 1;}
  67.   else if (bpp>4)  {bpp=8; sprite.mode = 28;}
  68.   else if (bpp>2)  {bpp=4; sprite.mode = 27;}
  69.   else if (bpp>1)  {bpp=2; sprite.mode = 26;}
  70.   else             {bpp=1; sprite.mode = 25;}
  71.  
  72.   sprite.leftHandWastage = 0;
  73.   sprite.height          = height_pix-1;
  74.   int bits=width_pix*bpp-1;
  75.   sprite.width           = bits/32;
  76.   sprite.lastBitUsed     = bits & 31;
  77.  
  78.   if (height_pix*width_pix > (1024*512))
  79.   {
  80.     int spriteSize=(bits/8)*height_pix;
  81.  
  82.     if (sprite.mode>>27)
  83.     {
  84.       int bits=(sprite.width*32) + sprite.lastBitUsed;
  85.       int pix=((bits+1) >> ((sprite.mode >> 27)-1));
  86.       spriteSize += (4*(pix + 31)/32);
  87.     }
  88.     else 
  89.       spriteSize*=2;
  90.     drawfile.ensureMem(spriteSize+(1024*4));
  91.   }
  92. }
  93.  
  94. //***************************************************************
  95.  
  96. void GuiDrawFileSprite::startImage()
  97. {
  98.   sprite.offsetToImage = drawfile.getSize() - 
  99.                          startPos -
  100.                          sizeof(GuiDrawFileGraphicsObject) -
  101.                          sizeof(GuiTransform);
  102.   if (sprite.offsetToMask==-1) sprite.offsetToMask = sprite.offsetToImage;
  103. }
  104.  
  105. //***************************************************************
  106.  
  107. void GuiDrawFileSprite::startMask()
  108. {
  109.   sprite.offsetToMask = drawfile.getSize() - 
  110.                         startPos -
  111.                         sizeof(GuiDrawFileGraphicsObject) -
  112.                         sizeof(GuiTransform);
  113. }
  114.  
  115. //***************************************************************
  116.  
  117. void GuiDrawFileSprite::startMaskFill()
  118. {
  119.   unsigned int words = sprite.width+1;
  120.   if (sprite.mode>>27)
  121.   {
  122.     int bits=(sprite.width*32) + sprite.lastBitUsed;
  123.     int pix=((bits+1) >> ((sprite.mode >> 27)-1));
  124.     words = (pix + 31)/32;
  125.   }
  126.   words*= (sprite.height+1);
  127.   startMask();
  128.   for (;words;words--) put(0u);
  129. }
  130.  
  131. //***************************************************************
  132.  
  133. void GuiDrawFileSprite::end(const GuiTransform& transform, const GuiBBox& bounds)
  134. {
  135.   if (drawfile.hasFailed()) return;
  136.  
  137.   GuiDrawFileGraphicsObject& object = *(GuiDrawFileGraphicsObject*)drawfile.getPtr(startPos);
  138.   object.typeId = OBJECT_ID;
  139.   object.size   = drawfile.getSize() - startPos;
  140.   object.bounds = bounds;
  141.  
  142.   *(GuiTransform*) ( (char*)(&object+1) ) = transform;
  143.  
  144.   sprite.size=object.size - sizeof(GuiDrawFileGraphicsObject) - sizeof(GuiTransform);
  145.  
  146.   *(GuiDrawFileSpriteBlock*)( (char*)(&object+1) + sizeof(GuiTransform) ) = sprite;
  147.   drawfile.addBBox(bounds);
  148.   drawfile.endObject();
  149. }
  150.